const { createGame, getGameInfo, updateGameInfo} = require('../service/game.service');

const {createLog} = require('../service/log.service');

const { databaseError } = require('../constant/err.type');

class GameController {

    async addGame(ctx, next){
        let {game_name, game_desc, game_detail, game_url, banner_url} = ctx.request.body;
        // 操作数据库
        try {
            let res = await createGame(game_name, game_desc, game_detail, game_url, banner_url);
            // 记录日志
            await createLog(ctx, `添加游戏成功，游戏名：${game_name}`);
            ctx.body = {
                code: 1,
                message: '游戏添加成功！',
                result: {
                    id: res.id,
                    game_name: res.game_name
                }
            };
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx)
        }
    }
    async updateGame(ctx, next){
        // 获取数据
        let {id, game_name, game_desc, game_detail, game_url, banner_url } = ctx.request.body;
        // 操作数据库
        try {
            let res = await updateGameInfo({id, game_name, game_desc, game_detail, game_url, banner_url});
            if(res){
                // 记录日志
                await createLog(ctx, `修改游戏成功，游戏名：${game_name}`);
                ctx.body = {
                    code : 1,
                    message: '游戏修改成功！',
                    result: {

                    }
                }
            }else {
                ctx.app.emit('error', databaseError, ctx);
            }
        }catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }
    }
    async queryGame(ctx, next){
        
        let { id, game_name, game_desc, flag, pageInfo} = ctx.request.query;
        try {
            let res  = await getGameInfo({id, game_name, game_desc, flag, pageInfo});
            ctx.body = {
                code : 1,
                message: '游戏查询成功！',
                data : res
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }

    }
    async queryGameDetail(ctx, next){
        let { id } = ctx.request.query;
        try {
            let res  = await getGameInfo({id});
            ctx.body = {
                code : 1,
                message: '游戏详情查询成功！',
                result : res
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }

    }
    async deleteGame(ctx, next){
        // 获取数据
        let {id, flag } = ctx.request.body;
        // 操作数据库
        try {
            let res = await updateGameInfo({id, flag});
            if(res){
                // 记录日志
                await createLog(ctx, `删除游戏成功，游戏ID:${id}`);
                ctx.body = {
                    code : 1,
                    message: '游戏删除成功！',
                    result: {

                    }
                }
            }else {
                ctx.app.emit('error', databaseError, ctx);
            }
        }catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }
    }
}

module.exports = new GameController();